package com.admin.controller;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.admin.controller.BaseController.Status;
import com.admin.model.Roles;
import com.admin.model.SysDict;
import com.admin.model.SysDictItem;

public class SysDictController extends BaseController {
	public static String TODICT = "/sysDict";
	
	// 查询数据字典
	public void index(){
		try {
			List<SysDict> list = SysDict.dao.list();
			setAttr("list",list);
			Long dictId = getParaToLong("dictId");
			if(null == dictId){
				dictId = getSessionAttr("dictId");
			}
			if(null != list && list.size() > 0){
				if(null == dictId){
					dictId = list.get(0).get("id");
				}
				List<SysDictItem> items = SysDictItem.dao.list(new String[]{"C_DICT_ID='"+ dictId +"'"});
				setAttr("items",items);
			}
			setAttr("id",dictId);
			removeSessionAttr("dictId");
			render("/jsp/sysDict/index.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 查询数据字典项
	public void getItemList(){
		try {
			List<SysDictItem> items = SysDictItem.dao.list(new String[]{"C_DICT_ID='"+ getPara("dictId") +"'"});
			setAttr("items",items);
			renderJsp("/jsp/sysDict/item_inc.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 跳往添加
	public void toAdd() {
		try {
			setAttr("dictId",getPara("dictId"));
			render("/jsp/sysDict/add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage(), e);
		}
	}
	
	// 编辑
	public void detail(){
		SysDictItem item = SysDictItem.dao.getObject(getParaToLong("id"));
		setAttr("sysDictItem",item);
		setAttr("dictId",item.get("C_DICT_ID"));
		render("/jsp/sysDict/add.jsp");
	}
	
	// 保存信息
	public void save(){
		try {
			SysDictItem item = getModel(SysDictItem.class);
			if (null == item.get("id")) {
				item.save();
			} else {
				item.update();
			}
			setSessionAttr("dictId",item.get("C_DICT_ID"));
			ajax(Status.success,TODICT,true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage(), e);
			ajax(Status.error,null,null);
		}
	}
	
	// 删除数据字典项
	public void delItem(){
		try {
			String[] ids = getParaValues("ids");
			boolean result = SysDictItem.dao.del(ids);
			if(result){
				ajax(Status.success,null,false);
			}else{
				ajax(Status.error,null,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			renderJson(ajax(Status.error,"删除失败"));
		}
	}
}
